//
//  HMSessionManger.m
//  weibo
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMSessionManger.h"

@implementation HMSessionManger

+(instancetype)shared{
    static HMSessionManger *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
        //可能需要设置响应的序列化器和请求的序列化器
        //@"application/json", @"text/json", @"text/javascript"
        _instance.responseSerializer.acceptableContentTypes = [_instance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
    });
    return _instance;
}

//申请数据
-(void)requestMethod:(httpMethod)method urlString:(NSString *)urlString parameters:(id)parameters finishedBlock:(void(^)(id finished,NSError* error))finishedBlock{
   
    void(^successBlock)(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        finishedBlock(responseObject,nil);
    };
    
    void(^failureBlock)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        finishedBlock(nil,error);
    };
    
    if (method == GET) {
        [self GET:urlString parameters:parameters progress:nil success:successBlock failure: failureBlock];
    }else{
        [self POST:urlString parameters:parameters progress:nil success:successBlock failure:failureBlock];
    }
}




@end
